package com.dao.user;

import com.dao.BaseDao;
import com.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao{
    /*登录Dao*/
    @Override
    public User getLoginUser(Connection connection, String userPhone) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;

        System.out.println("UserDao中："+userPhone);
        if(connection!=null){
            String sql = "select * from user where userPhone=?";
            Object[] o = {userPhone};
            resultSet = BaseDao.excute(connection,preparedStatement,resultSet,sql,o);
            while(resultSet.next()){
                user = new User();
                user.setUserId(resultSet.getInt("userId"));
                System.out.println(resultSet.getInt("userId"));
                user.setUserPhone(resultSet.getString("userPhone"));
                user.setUserName(resultSet.getString("userName"));
                user.setUserPassword(resultSet.getString("userPassword"));
                user.setUserAddress(resultSet.getString("userAddress"));
                user.setUserQq(resultSet.getString("userQq"));
            }
        }
        BaseDao.closeResource(null,preparedStatement,resultSet);
        System.out.println("返回User成功");
        return user;
    }

    @Override
    public int UserchangePassword(Connection connection, int userId,String userPassword) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int flag = 0;

        if (connection != null){
            String sql = "update user set userPassword = ? where userId = ?";
            Object []o = {userPassword,userId};
            flag = BaseDao.excute(connection,preparedStatement,sql,o);
        }
        BaseDao.closeResource(null,null,null);
        return flag;
    }

    /*注册Dao*/
    @Override
    public int RegisterUser(Connection connection, String userName, String userPhone, String userPassword, String userQQ, String userAddress) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int Rows = -1;//返回修改的行数
        System.out.println("注册的数据：" +" | userName = "+userName+" | userPhone = "+userPhone+" | userPassword = "+userPassword+" | userQQ = "+userQQ+" | "+" | userAddress = "+userAddress);
        if(connection != null){
            String sql = "insert into user(userName,userPhone,userPassword,userQQ,userAddress) " +
                    "values(?,?,?,?,?)";
            Object []o = {userName,userPhone,userPassword,userQQ,userAddress};

            int flag = selectUserPhone(connection,userPhone);
            if(flag == 1){
                Rows = BaseDao.excute(connection,preparedStatement,sql,o);
            }else{
                return Rows;
            }
        }
        BaseDao.closeResource(null,null,null);
        return Rows;
    }







    /*判断手机号是否重复*/
    public int selectUserPhone(Connection connection,String userPhone) throws SQLException {
        int flag = 1;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select userPhone from user";
        Object o[] = {};
        resultSet = BaseDao.excute(connection,preparedStatement,resultSet,sql,o);
        while (resultSet.next()){
            if(resultSet.getString("userPhone").equals(userPhone)){
                System.out.println("手机号重复");
                flag = 2;
                break;
            }
        }
        return flag;
    }

    public String oldPassword(int userId) throws SQLException {
        Connection connection = null;
        connection = BaseDao.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select userPassword from user where userId = ?";
        Object []o = {userId};
        String oldPassword = null;
        resultSet = BaseDao.excute(connection,preparedStatement,resultSet,sql,o);
        if (resultSet.next()){
            oldPassword = resultSet.getString("userPassword");
        }
        BaseDao.closeResource(connection,null,resultSet);
        return oldPassword;
    }

}
